package org.management.system.controller;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.management.base.BaseController;
import org.management.base.data.PermissionCode;
import org.management.base.data.Result;
import org.management.config.shiro.manager.TokenManager;
import org.management.system.entity.FileEntity;
import org.management.system.entity.RoleEntity;
import org.management.system.entity.UserEntity;
import org.management.system.service.FileService;
import org.management.system.service.RoleService;
import org.management.utils.WordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(tags = "文件模块")
@RequestMapping(value="/file")
public class FileController extends BaseController<FileEntity>{
	
	private final FileService fileService;
	
	private final RoleService roleService;

	@Autowired
	public FileController(FileService fileService, RoleService roleService) {
		this.fileService = fileService;
		this.roleService = roleService;
	}

	/**
	 * 文件上传
	 * @param file 文件
	 * @param parentId 父级文件id
	 * @return ResultData
	 */
	@ApiOperation("文件上传")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "file",value = "文件流",required = true,dataType = "MultipartFile"),
		@ApiImplicitParam(name = "parentId",value = "父级文件夹",required = true,dataType = "String")
	})
	@PostMapping(value="/list/upload")
	@RequiresPermissions(value = PermissionCode.USER_WRITE)
	public Result fileUpload(@RequestParam("file") MultipartFile file, String parentId) {
		try {
			this.fileService.saveFile(file, parentId);
		} catch (IOException e) {
			e.printStackTrace();
			return Result.failure("IO流获取失败");
		}
		return Result.success("上传成功");
	}
	
	/**
	 * 生成word
	 */
	@PostMapping(value="/generate/word")
	@ApiOperation("word文档生成")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "response",value = "响应流",required = true,dataType = "HttpServletResponse")
	})
	@RequiresPermissions(value = PermissionCode.USER_WRITE)
	public void generateWord(HttpServletResponse response) {
		UserEntity user = TokenManager.getToken();
		List<RoleEntity> roles = this.roleService.getRoleByUserId(user.getId());
		Map<String, Object> resourceMap = new HashMap<>(roles.size());
		for(RoleEntity role : roles) {
			List<Map<String,Object>> list = new ArrayList<>();
			int i = 0;
			while(i < 10) {
				i++;
				Map<String,Object> map = new HashMap<>(16);
				map.put("menuNo", 12);
				map.put("menuName", 222);
				list.add(map);
			}
			resourceMap.put(role.getId(), list);
		}
		Map<Object, Object> dataMap = new HashMap<>();
		dataMap.put("user", user);
		dataMap.put("roles", roles);
		dataMap.put("resourceMap", resourceMap);
		WordUtils.createWord(dataMap, "xml/用户信息.xml", "F:", user.getNickName()+"权限信息.docx");
		WordUtils.downFile("F:/"+user.getNickName()+"权限信息.docx", response);
	}
}
